package com.karst.webnotes.chapter04
{
	import flash.events.Event;
	
	import org.aswing.AsWingManager;
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.materials.special.CompositeMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	
	internal class CompositeMaterialExample extends BasicView
	{
		public function CompositeMaterialExample()
		{
			super() ;
			AsWingManager.getStage().frameRate = 24 ;
			createView() ;
		}

		private var bmpMaterial : BitmapFileMaterial = null ;
		private var wmMaterial : WireframeMaterial = null ;
		private var material : CompositeMaterial = null ;
		private var plane : Plane = null ;
		private function createView() : void
		{
			if( bmpMaterial == null )
			{
				bmpMaterial = new BitmapFileMaterial( "01.jpg" ) ;
				//为材质加载图片添加监听，加载完成即处理
				bmpMaterial.addEventListener( FileLoadEvent.LOAD_COMPLETE, loadedHandler ) ;
			}
			
			if( wmMaterial == null )
				wmMaterial = new WireframeMaterial() ;
			
			if( material == null )
			{
				material = new CompositeMaterial() ;
				material.doubleSided = true ;
				material.smooth = true ;
				material.interactive = true ;
				material.addMaterial( bmpMaterial ) ;
				material.addMaterial( wmMaterial ) ;
			}
			
			//一定要设置平面大小，否则位图文件材质会被设为0*0，不能拉伸
			if( plane == null )
				plane = new Plane( material, 400, 600, 2, 2 ) ;
			
			scene.addChild( plane ) ;
		}
		
		private function loadedHandler( event : FileLoadEvent ) : void
		{
			startRendering() ;
		}
		
		override protected function onRenderTick(event:Event=null):void
		{
			super.onRenderTick() ;
			if( plane )
				plane.localRotationY-- ;
		}
	}
}